import worker, { ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@ohos.worker';

const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
import hilog from '@ohos.hilog';


import * as ArkGame from '../arkgame/arkgame';
import { Mesh, VertexAttribute } from '../arkgame/arkgame';


enum CallBackType {
  Touch = 0,
  UpdateFrame = 1,
}


/**
 * Defines the event handler to be called when the worker thread receives a message sent by the host thread.
 * The event handler is executed in the worker thread.
 *
 * @param e message data
 */

var helper:ArkGame.Helper;
workerPort.onmessage = function (e: MessageEvents) {
  //游戏在这个独立的线程写逻辑，要不然Update会卡住
  if (e.data == "start") {
    hilog.info(0xffff, "===>>", "---Worker Start=---");
    helper=new ArkGame.Helper();
    helper.Gfx_Init(onInit, onUpdate);

  }
}

/**
 * Defines the event handler to be called when the worker receives a message that cannot be deserialized.
 * The event handler is executed in the worker thread.
 *
 * @param e message data
 */
workerPort.onmessageerror = function (e: MessageEvents) {
}

/**
 * Defines the event handler to be called when an exception occurs during worker execution.
 * The event handler is executed in the worker thread.
 *
 * @param e error message
 */
workerPort.onerror = function (e: ErrorEvent) {
}


//var regoutput: ArrayBuffer;

var mesh:Mesh;//一个模型

var c:ArkGame.Color =new ArkGame.Color();
function onInit(): void {
  hilog.info(0xffff, "===>>", "GFX OnInit");

  c.r=0;
  c.g=1;
  c.b=1;
  c.a=1;

  //创建一个4顶点，6索引的模型
  var attr =new VertexAttribute(true,false,false,false,false);
  //mesh=helper.Gfx_MeshCreate(attr,4,6);
}

function onUpdate(): void {
  if (helper.frameID % 100 == 0) {
    hilog.info(0xffff, "===>>", "GFX OnUpdate"
      + " fid=" + helper.frameID + " delta=" + helper.delta);
  }
  c.r+=helper.delta;
  if(c.r>1)c.r=0;
  helper.Clear(c);


}